home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / zoo tutorial / module 7- switching compositors / source / animalpane.java next >
Encoding:
Java Source  |  2000-06-23  |  14.9 KB  |  486 lines

  1. import java.awt.Dimension;
  2. import java.io.IOException;
  3. import java.io.FileNotFoundException;
  4.  
  5. import quicktime.app.QTFactory;
  6. import quicktime.app.anim.Compositor;
  7. import quicktime.app.anim.TwoDSprite;
  8. import quicktime.app.anim.SWController;
  9. import quicktime.app.event.QTActionEvent;  
  10. import quicktime.app.event.QTActionListener;
  11. import quicktime.app.event.QTMouseTargetController; 
  12. import quicktime.app.image.GraphicsImporterDrawer; 
  13. import quicktime.app.image.ImagePresenter; 
  14. import quicktime.app.image.ImageDataSequence;
  15. import quicktime.app.image.ImageUtil;
  16. import quicktime.app.ui.ReleaseButton;
  17. import quicktime.app.ui.ButtonActivator;
  18. import quicktime.app.players.MoviePresenter;
  19. import quicktime.app.players.QTPlayer;
  20.  
  21. import quicktime.io.QTFile;
  22. import quicktime.io.OpenMovieFile;
  23.  
  24. import quicktime.qd.QDRect;
  25. import quicktime.qd.QDGraphics;
  26. import quicktime.qd.QDColor; 
  27. import quicktime.qd.QDConstants; 
  28.  
  29. import quicktime.std.StdQTConstants;
  30. import quicktime.std.movies.Movie; 
  31. import quicktime.std.image.Matrix;
  32. import quicktime.std.image.GraphicsMode;
  33.  
  34. import quicktime.QTSession;
  35. import quicktime.QTException; 
  36.  
  37. /**
  38.  * QTZoo Module 6 - Advanced Compositing and Event Handling
  39.  * This application requires QuickTime for Java
  40.  *
  41.  * @author Levi Brown
  42.  * @author Michael Hopkins
  43.  * @author Apple Computer, Inc.
  44.  * @version 5.1 11/15/1999
  45.  *
  46.  * Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  47.  *    
  48.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  49.  *                ("Apple") in consideration of your agreement to the following terms, and your
  50.  *                use, installation, modification or redistribution of this Apple software
  51.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  52.  *                please do not use, install, modify or redistribute this Apple software.
  53.  *
  54.  *                In consideration of your agreement to abide by the following terms, and subject
  55.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  56.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  57.  *                reproduce, modify and redistribute the Apple Software, with or without
  58.  *                modifications, in source and/or binary forms; provided that if you redistribute
  59.  *                the Apple Software in its entirety and without modifications, you must retain
  60.  *                this notice and the following text and disclaimers in all such redistributions of
  61.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  62.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  63.  *                Apple Software without specific prior written permission from Apple.  Except as
  64.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  65.  *                are granted by Apple herein, including but not limited to any patent rights that
  66.  *                may be infringed by your derivative works or by other works in which the Apple
  67.  *                Software may be incorporated.
  68.  *
  69.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  70.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  71.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  72.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  73.  *                COMBINATION WITH YOUR PRODUCTS.
  74.  *
  75.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  76.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  77.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  78.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  79.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  80.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  81.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  82.  * 
  83.  */
  84. public class AnimalPane extends ZooPane
  85. {
  86.     /**
  87.      *  Public default constructor
  88.      *  Creates the map button, and sets up the listeners
  89.      */
  90.     public AnimalPane()
  91.     {
  92.         QDRect size = new QDRect(MainFrame.WIDTH, MainFrame.HEIGHT);
  93.         try
  94.         {
  95.             loadSound("data/zebra/Zebra.au");
  96.  
  97.             QDGraphics gw = new QDGraphics (size);
  98.             compositor = new Compositor (gw, QDColor.white, 30, 1);
  99.                     
  100.             displayImage( "data/zebra/ZebraBackground.jpg", 6, 5, 110 );        
  101.             
  102.             addText( "data/zebra/Zebra.txt", 5, 15, 65, 415, 220 );
  103.             
  104.             addMovie( "data/zebra/Zebra.mov", 2, 425, 65 );
  105.             
  106.             ctr = new QTMouseTargetController( false );
  107.     
  108.             ctr.addQTMouseListener( new PaneMouseListener( QDColor.lightGray, QDColor.white, QDColor.darkGray));
  109.             compositor.addController( ctr );
  110.  
  111.             currentSprite = CreateRollover( "data/MapButton.jpg", 5, 12, 406 );
  112.             
  113.             playSound();        
  114.         }
  115.         catch ( QTException qte )
  116.         {
  117.             qte.printStackTrace();
  118.         }
  119.     }    
  120.     
  121.     /**
  122.      * Listener object for the Movie controller buttons
  123.      * Called when the button is released
  124.      */
  125.     public class ButtonListener implements QTActionListener    
  126.     {
  127.         /**
  128.          * Called when the button is released
  129.          * @param ???
  130.          */
  131.         public void actionPerformed( QTActionEvent event ) 
  132.         {
  133.             try
  134.             {
  135.                 Object source = event.getSource();
  136.                 
  137.                 if (source.equals(playButton))            // Play >
  138.                 {
  139.                     if ( md.getRate() == 0 )
  140.                     {
  141.                         md.setRate( 1 );
  142.                     }
  143.                     stopButton.setCurrentImage( stopRel );
  144.                     stopButton.setReleasedImage( stopRel );
  145.                     playButton.setCurrentImage( playPlaying );
  146.                     playButton.setReleasedImage( playPlaying );
  147.                 }                
  148.                 else if (source.equals(stopButton))        // Stop []
  149.                 {
  150.                     if ( md.getRate() == 1 )
  151.                     {
  152.                         md.setRate( 0 );
  153.                     }
  154.                     stopButton.setCurrentImage( stopDeactive );
  155.                     stopButton.setReleasedImage( stopDeactive );
  156.                     playButton.setCurrentImage( playRel );
  157.                     playButton.setReleasedImage( playRel );
  158.                 }
  159.                 else if (source.equals(rewindButton))    // Rewind <<
  160.                 {
  161.                     md.setTime(0);
  162.                 }
  163.             }
  164.             catch (QTException exc )
  165.             {
  166.                 exc.printStackTrace();
  167.             }
  168.         }
  169.     }
  170.     
  171.     /**
  172.      * Loads a graphics file from a relative path and creates an ImagePresenter object
  173.      * @param ???
  174.      */
  175.     public ImagePresenter makePresenterFromFile( String name )
  176.     {
  177.         try
  178.         {
  179.             QTFile file = new QTFile (QTFactory.findAbsolutePath (name));
  180.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer(file);
  181.             return ImagePresenter.fromGraphicsImporterDrawer(drawer);
  182.         }
  183.         catch (IOException e)
  184.         {
  185.             e.printStackTrace();
  186.         }
  187.         catch (QTException e)
  188.         {
  189.             e.printStackTrace();
  190.         }
  191.         return null;    
  192.     }
  193.     
  194.     /**
  195.      * Called to do any setup after being set as the client of the QTCanvas.
  196.      * May be used to start effects running, movies playing, etc.
  197.      */
  198.     public void start()
  199.     {
  200.         buttonController.addQTMouseListener(buttonActivator);
  201.         try
  202.         {
  203.             stopButton.setCurrentImage(  stopRel );        // Stop button should not have focus
  204.             playButton.setCurrentImage(  playPlaying );  // Play button should get focus (blue arrow)
  205.             playButton.setReleasedImage( playPlaying );
  206.         }
  207.         catch( QTException e )
  208.         {
  209.             e.printStackTrace();
  210.         }
  211.         try
  212.         {
  213.             md.setRate(1);                                // start the player
  214.         }
  215.         catch( QTException e )
  216.         {
  217.             e.printStackTrace();
  218.         }
  219.         compositor.getTimer().setRate(1);                // start the compositor
  220.     }
  221.     
  222.     /**
  223.      * Called to do clean up after being removed as the client of the QTCanvas.
  224.      * Should be used to stop effects running, movies playing, etc.
  225.      */
  226.     public void stop()
  227.     {
  228.         buttonController.removeQTMouseListener(buttonActivator);
  229.         compositor.getTimer().setRate(0);
  230.         try
  231.         {
  232.             md.setRate(0);
  233.         }
  234.         catch( QTException e )
  235.         {
  236.             e.printStackTrace();
  237.         }
  238.     }
  239.     
  240.     /**
  241.      *  Plays the sound file associated with the current AnimalPane
  242.      */
  243.     public void playSound()
  244.     {
  245.         if (player == null)
  246.             return;
  247.  
  248.         try
  249.         {
  250.             player.setTime(0);            //Start the sound at the beginning
  251.             player.startTasking();         //Make sure the player gets time to play the sound
  252.             player.setRate(1);             //Start playing
  253.         }
  254.         catch (QTException exc)
  255.         {
  256.             exc.printStackTrace();
  257.         }
  258.     }
  259.     
  260.     /**
  261.      * Creates a Movie from the specified path and adds it
  262.      * to the compositor in the hardcoded location.
  263.      * @param moviePath the relative path to the movie file to add.
  264.      * @param layer, the layer of the compositor to add the movie to.
  265.      * @param x the x coordinate location.
  266.      * @param y the y coordinate location.
  267.      * @see #makeMovie
  268.      */
  269.     protected void addMovie(String moviePath, int layer, int x, int y)
  270.     {
  271.         try
  272.         {
  273.             Movie m = makeMovie (new QTFile (QTFactory.findAbsolutePath (moviePath)));
  274.             md = new MoviePresenter (m);
  275.             md.setLocation (x, y);
  276.             // Add the Movie Controls
  277.             
  278.             playRel     = makePresenterFromFile( "data/Play.jpg" );            // Play >
  279.             playPress   = makePresenterFromFile( "data/PlayPressed.jpg" );
  280.             playPlaying = makePresenterFromFile( "data/Playing.jpg" );
  281.             playButton = new MovieButton( playRel, playPress, playPlaying );
  282.             playButton.setLabel ("Play");
  283.             playButton.setLocation( x + 68, y + 152 );
  284.             
  285.             stopRel      = makePresenterFromFile( "data/Stop.jpg" );        // Stop []
  286.             stopPress    = makePresenterFromFile( "data/StopPressed.jpg" );
  287.             stopDeactive = makePresenterFromFile( "data/Stopped.jpg" );
  288.             stopButton  = new MovieButton( stopRel, stopPress, stopDeactive );
  289.             stopButton.setLabel ("Stop");
  290.             stopButton.setLocation( x + 136, y + 152 );
  291.             
  292.             rewRel      = makePresenterFromFile( "data/Rewind.jpg" );        // Rewind <<
  293.             rewPress    = makePresenterFromFile( "data/RewindPressed.jpg" );
  294.             rewDeactive = rewRel;
  295.             rewindButton = new MovieButton( rewRel, rewPress, rewDeactive );
  296.             rewindButton.setLabel( "Rewind" );
  297.             rewindButton.setLocation( x, y + 152 );
  298.             
  299.             ButtonListener bl = new ButtonListener();                        // create a new listener for the buttons
  300.             
  301.             playButton.addActionListener(bl);
  302.             stopButton.addActionListener(bl);
  303.             rewindButton.addActionListener(bl);
  304.             
  305.             compositor.addMember(md, layer);                                // add the movie presenter
  306.             compositor.addMember(playButton);                                //     and buttons to the compositor
  307.             compositor.addMember(stopButton);
  308.             compositor.addMember(rewindButton);
  309.             
  310.             buttonController = new QTMouseTargetController (false);    
  311.             buttonController.addMember(playButton);
  312.             buttonController.addMember(stopButton);
  313.             buttonController.addMember(rewindButton);
  314.         
  315.             compositor.addController(buttonController);
  316.             buttonActivator = new ButtonActivator();
  317.             
  318.             md.setRate(1);                                                    // start the movie        
  319.         }
  320.         catch (QTException exc)
  321.         {
  322.             exc.printStackTrace();
  323.         }
  324.         catch ( IOException exc )
  325.         {
  326.             exc.printStackTrace();
  327.         }    
  328.     }
  329.     
  330.     /**
  331.      * Utility routine loads an image from a file, renders it, and adds it to the compositor
  332.      * @param filename the name of the file to be displayed
  333.      * @param layer the layer in the compositor where the image will be displayed
  334.      * @param x the x coordinate of the top left corner where the image is to be displayed
  335.      * @param y the y coordinate of the top left corner where the image is to be displayed
  336.      */
  337.     protected void displayImage( String filename, int layer, int x, int y )
  338.     {
  339.         try
  340.         {    
  341.             QTFile file = new QTFile (QTFactory.findAbsolutePath (filename));
  342.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer(file);
  343.             ImagePresenter presenter = ImagePresenter.fromGraphicsImporterDrawer(drawer);
  344.             presenter.setLocation( x, y );
  345.             compositor.addMember(presenter, layer );
  346.         }
  347.         catch (QTException exc)
  348.         {
  349.             exc.printStackTrace();
  350.         }
  351.         catch (IOException exc)
  352.         {
  353.             exc.printStackTrace();
  354.         }        
  355.     }
  356.     
  357.     /**
  358.      * Reads a text file from the disk and displays it in the compositor
  359.      * @param textPath the relative path where the text is located
  360.      * @param layer, the layer of the compositor to add the text to.
  361.      * @param x the x coordinate location.
  362.      * @param y the y coordinate location.
  363.      * @param width the width of the area to display the text.
  364.      * @param height the height of the area to display the text.
  365.      */
  366.     protected void addText(String textPath, int layer, int x, int y, int width, int height)
  367.     {
  368.         try
  369.         {
  370.             TextPresenter text = new TextPresenter(textPath, new Dimension(width, height));
  371.             
  372.             ImagePresenter presenter = text.getPresenter();
  373.             Matrix theMatrix = new Matrix();
  374.             theMatrix.translate( (float) x, (float) y );
  375.             presenter.setMatrix(theMatrix);
  376.             compositor.addMember(presenter, layer);
  377.         }
  378.         catch ( QTException exc )
  379.         {
  380.             exc.printStackTrace();
  381.         }
  382.         catch ( FileNotFoundException exc )
  383.         {
  384.             exc.printStackTrace();
  385.         }    
  386.     }
  387.     
  388.     /**
  389.      * Loads the specified sound file from disk and prepares it for playing
  390.      * @param ???
  391.      */
  392.     protected void loadSound(String soundPath)
  393.     {
  394.         try
  395.         {
  396.             String soundLocation = QTFactory.findAbsolutePath(soundPath).getPath();
  397.             //this call works with a file://, http://, rtsp://located movie
  398.             player = (QTPlayer)QTFactory.makeDrawable ("file://" + soundLocation);
  399.         }
  400.         catch (IOException exc)
  401.         {
  402.             exc.printStackTrace();
  403.         }
  404.         catch (QTException exc)
  405.         {
  406.             exc.printStackTrace();
  407.         }
  408.     }
  409.     
  410.  
  411.     /**
  412.      * Opens the movie file and sets it up to be played.
  413.      * @param f a QTFile representing the movie to initialize.
  414.      * @return the movie, ready to play
  415.      * @see #addMovie
  416.      */
  417.     protected Movie makeMovie (QTFile f) throws IOException, QTException
  418.     {
  419.         OpenMovieFile movieFile = OpenMovieFile.asRead(f);
  420.         Movie m = Movie.fromFile (movieFile);
  421.         m.getTimeBase().setFlags (StdQTConstants.loopTimeBase);    
  422.         return m;    
  423.     }
  424.  
  425.     /**
  426.      *    Creates a TwoDSprite that behaves as a simple rollover button (such as the map button)
  427.      */
  428.     protected TwoDSprite CreateRollover( String filename, int layer, int x, int y )
  429.     {
  430.         TwoDSprite sprite = null;
  431.         try
  432.         {
  433.             QTFile                 file = new QTFile( QTFactory.findAbsolutePath( filename ));
  434.             ImagePresenter         presenter = ImagePresenter.fromFile( file );
  435.             ImageDataSequence     dataSeq = new ImageDataSequence (presenter.getDescription());
  436.  
  437.             dataSeq.addMember( presenter.getImage() );
  438.  
  439.             if ((QTSession.isCurrentOS(QTSession.kWin32) && QTSession.getQTMajorVersion() == 3) == false)    //doesn't work on QT3.0.2 on Win
  440.                 dataSeq = ImageUtil.makeTransparent ( dataSeq, QDColor.white );        // recompress the image using white as the transparent color
  441.  
  442.             Matrix theMatrix = new Matrix();
  443.             theMatrix.translate( (float) x, (float) y );
  444.             sprite = new TwoDSprite( dataSeq, theMatrix, true, 10 );
  445.  
  446.             GraphicsMode normalGM = new GraphicsMode (QDConstants.blend, QDColor.lightGray);
  447.             sprite.setGraphicsMode (normalGM);
  448.             compositor.addMember(sprite, layer);
  449.             ctr.addMember( sprite );
  450.         }
  451.         catch ( QTException exc )
  452.         {
  453.             exc.printStackTrace();
  454.         }
  455.         catch ( IOException exc )
  456.         {
  457.             exc.printStackTrace();
  458.         }
  459.         
  460.         return sprite;    
  461.     }
  462.     
  463.     protected QTPlayer player;
  464.     protected MoviePresenter md;
  465.     
  466.     protected ImagePresenter playRel;
  467.     protected ImagePresenter playPress;
  468.     protected ImagePresenter playPlaying;
  469.     protected MovieButton playButton;
  470.             
  471.     protected ImagePresenter stopRel;
  472.     protected ImagePresenter stopPress;
  473.     protected ImagePresenter stopDeactive;
  474.     protected MovieButton stopButton;
  475.     
  476.     protected ImagePresenter rewRel;
  477.     protected ImagePresenter rewPress;
  478.     protected ImagePresenter rewDeactive;
  479.     protected MovieButton rewindButton;
  480.  
  481.     protected TwoDSprite currentSprite;
  482.     
  483.     protected QTMouseTargetController buttonController;
  484.     protected ButtonActivator buttonActivator;
  485. }
  486.